﻿using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Routing;
using Journal.Common.Extensions;

namespace Journal.Common.Helpers.HtmlControls
{
    public static class LinkControls
    {
        public static MvcHtmlString LinkControl(this HtmlHelper html, string value, string url)
        {
            return LinkControl(value, url);
        }

        public static MvcHtmlString LinkControl(this HtmlHelper html, string value, string url, object htmlAttributes)
        {
            return LinkControl(value, url, htmlAttributes);
        }

        public static MvcHtmlString LinkControl(this HtmlHelper html, string value, string url, object htmlAttributes, IDictionary<string, string> dataParams)
        {
            return LinkControl(value, url, htmlAttributes, dataParams);
        }

        public static MvcHtmlString LinkControl(this HtmlHelper html, string value, string url, Dictionary<string, string> dataParams)
        {
            return LinkControl(value, url, dataParams);
        }

        public static MvcHtmlString ImageLinkControl(this HtmlHelper html, string linkUrl, string imageSrc)
        {
            TagBuilder imageLink = ImageLink(linkUrl, imageSrc);
            return new MvcHtmlString(imageLink.ToString(TagRenderMode.Normal));
        }

        public static MvcHtmlString ImageLinkControl(this HtmlHelper html, string linkUrl, string imageSrc, object htmlAttributes)
        {
            return ImageLinkControl(linkUrl, imageSrc, htmlAttributes);
        }

        public static MvcHtmlString LinkControl(string value, string url)
        {
            TagBuilder link = Link(value, url);
            return new MvcHtmlString(link.ToString(TagRenderMode.Normal));
        }

        public static MvcHtmlString LinkControl(string value, string url, object htmlAttributes)
        {
            TagBuilder link = Link(value, url);
            link.MergeAttributes(new RouteValueDictionary(htmlAttributes));
            return new MvcHtmlString(link.ToString(TagRenderMode.Normal));
        }

        public static MvcHtmlString LinkControl(string value, string url, object htmlAttributes, IDictionary<string, string> dataParams)
        {
            TagBuilder link = Link(value, url);
            link.MergeAttributes(new RouteValueDictionary(htmlAttributes));
            link = link.InsertDataAttributes(dataParams);
            return new MvcHtmlString(link.ToString(TagRenderMode.Normal));
        }

        public static MvcHtmlString LinkControl(string value, string url, Dictionary<string, string> dataParams)
        {
            TagBuilder link = Link(value, url);
            link = link.InsertDataAttributes(dataParams);
            return new MvcHtmlString(link.ToString(TagRenderMode.Normal));
        }

        public static MvcHtmlString ImageLinkControl(string linkUrl, string imageSrc)
        {
            TagBuilder imageLink = ImageLink(linkUrl, imageSrc);
            return new MvcHtmlString(imageLink.ToString(TagRenderMode.Normal));
        }

        public static MvcHtmlString ImageLinkControl(string linkUrl, string imageSrc, object htmlAttributes)
        {
            TagBuilder imageLink = ImageLink(linkUrl, imageSrc);
            imageLink.MergeAttributes(new RouteValueDictionary(htmlAttributes));
            return new MvcHtmlString(imageLink.ToString(TagRenderMode.Normal));
        }

        public static TagBuilder Link(string value, string url)
        {
            TagBuilder link = new TagBuilder("a");
            link.Attributes.Add("href", url);
            link.InnerHtml = value;
            return link;
        }

        public static TagBuilder Link(string value, string url, object attributes)
        {
            TagBuilder link = Link(value, url);
            link.MergeAttributes(new RouteValueDictionary(attributes));
            return link;
        }

        public static TagBuilder Link(string value, string url, object attributes, IDictionary<string, string> dataParams)
        {
            TagBuilder link = Link(value, url);
            if (attributes != null)
            {
                link.MergeAttributes(new RouteValueDictionary(attributes));
            }

            if(dataParams != null)
            {
                link = link.InsertDataAttributes(dataParams);
            }

            return link;
        }

        private static TagBuilder ImageLink(string imageSrc, string linkUrl)
        {
            TagBuilder link = Link(string.Empty, linkUrl);
            TagBuilder image = new TagBuilder("img");
            image.Attributes.Add("src", imageSrc);
            link.InnerHtml += image.ToString(TagRenderMode.Normal);
            return link;
        }
    }
}